package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.PageData;
import com.example.pojo.ResultBean;
import com.example.pojo.SysOffice;
import com.example.service.ISysOfficeService;
import com.example.vo.SysOfficeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 机构表 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/sys-office")
public class SysOfficeController {
    @Autowired
    private ISysOfficeService sysOfficeService;

    /**
     * 查询全部机构
     * @return
     */
    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        return ResultBean.success(sysOfficeService.list());
    }

    /**
     * 分页条件查询
     * @param pageData
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ResultBean list(PageData pageData, @RequestParam Map<String,Object> map){
        map.forEach((k,v) -> System.out.println(k + ":" + v));
        Page<SysOfficeVo> page = new Page<>(pageData.getPageNum(),pageData.getPageSize());
        sysOfficeService.queryByCondition(page,map);
        return ResultBean.success(page);
    }

    /**
     * 新增或修改
     * @param sysOffice
     * @return
     */
    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody SysOffice sysOffice){
        System.out.println("新增或修改的数据：" + sysOffice);
        boolean rs = sysOfficeService.saveOrUpdate(sysOffice);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的id:" + id);
        boolean rs = sysOfficeService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("queryById/{id}")
    public ResultBean queryById(@PathVariable("id") Long id){
        return ResultBean.success(sysOfficeService.getById(id));
    }

}
